<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
         <!-- 搜索 -->
        <el-form :model="requestParameters" ref="requestParameters">
          <div class="filter-container">
           <el-form-item>
             目录名称 :
              <el-input
                style="width: 200px;"
                class="filter-item"
                v-model="requestParameters.directoryName"
                prop="directoryName"
              ></el-input>
              &nbsp;
              &nbsp;
              状态 :
              <el-select
                prop="state"
                placeholder="请选择"
                style="width: 100px;"
                class="filter-item"
                v-model="requestParameters.state"
              >
              <el-option
                v-for="item in status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                ></el-option>
              </el-select>
              &nbsp;
              &nbsp;
              <el-button
                class="filter-item"
                size="small"
                type="primary"
                icon="el-icon-search"
                @click="serschSub"
              >搜索</el-button>
              <el-button class="filter-item" size="small" type="primary" @click="resetForm">重置</el-button>
            </el-form-item>
            <div class="butAdd">
              <el-link type="primary" :href="'#/subjects/list'" :underline="false">
            <el-button
              v-show="this.$route.query.id"
              class="filter-item"
              size="small"
              style="margin-left: 20px;"
              type="primary"
              :underline="false"
              icon="el-icon-s-home"
            >返回学科</el-button>
            </el-link>
            <el-button
              class="filter-item"
              size="small"
              style="margin-left: 10px;"
              @click="handleChange('add')"
              :underline="false"
              type="primary"
              icon="el-icon-edit"
            >新增目录</el-button>
            </div>
          </div>
        </el-form>
      <span v-if= "this.$route.query.id">
       <Breadcrumb>
      {{this.$route.query.name}}
       > 目录管理
       </Breadcrumb>
        </span>
        <!-- 数据记录 -->
        <el-alert
          v-if="alertText !== ''"
          :title="alertText"
          type="info"
          class="alert"
          :closable="false"
          show-icon
        ></el-alert>
        <!-- end -->
                <!-- 数据 -->
        <el-table
          v-loading="listLoading"
          element-loading-text="给我一点时间"
          :data="dataList"
          highlight-current-row
          style="width: 100%"
          border>
         <el-table-column  align="center" label="序号">
            <template slot-scope="scope">
              <span>{{scope.row.id}}</span>
            </template>
          </el-table-column>
          <el-table-column  align="center" label="目录名称">
            <template slot-scope="scope">
              <span>{{scope.row.directoryName}}</span>
            </template>
          </el-table-column>
          <el-table-column  align="center" label="创建者">
            <template slot-scope="scope">
              <span>{{scope.row.username}}</span>
            </template>
          </el-table-column>
          <el-table-column  width="190" align="center" label="创建日期">
            <template slot-scope="scope">
              <span>{{scope.row.addDate| parseTimeByString("{y}-{m}-{d} {h}:{m}:{s}")}}</span>
            </template>
          </el-table-column>
            <el-table-column  align="center" label="面试题数量">
            <template slot-scope="scope">
              <span>{{scope.row.totals}}</span>
            </template>
          </el-table-column>
          <el-table-column  align="center" label="状态">
            <template slot-scope="scope">
              <span>{{scope.row.state ? '启用' : '禁用'}}</span>
            </template>
          </el-table-column>
          <!-- 操作按钮 -->
         <el-table-column
            align="center"
            label="操作"
            width="280"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope" class="Fsize">
               <el-link  type="primary" :underline="false"
               @click="statusChange(scope.row)">
               {{scope.row.state ? '禁用': '开启'}}
               </el-link>&nbsp;&nbsp;&nbsp;&nbsp;
               <el-link :type="scope.row.state ? 'info':'primary' "
               :underline="false" :disabled="scope.row.state ? true: false"
               @click="handleChange(scope.row.id)">修改</el-link>&nbsp;&nbsp;&nbsp;&nbsp;
               <el-link :type="scope.row.state ? 'info':'primary'"
               :underline="false"
               :disabled="scope.row.state ? true: false"
               @click="removeSub(scope.row.id)">删除</el-link>&nbsp;&nbsp;&nbsp;&nbsp;
            </template>
          </el-table-column>
        </el-table>
        <!-- end -->
        <!-- 分页 -->
        <div class="pagination">
           <el-pagination
            background
              @size-change="handleSizeChange"
              v-loading="listLoading"
              element-loading-text="拼命加载中"
              element-loading-spinner="el-icon-loading"
              @current-change="handleCurrentChange"
              :current-page="Number(requestParameters.page)"
              :total="Number(total)"
              :page-size="Number(requestParameters.pagesize)"
              :page-sizes="[10,20,30,50]"
              :hide-on-single-page="true"
              layout="sizes, prev, pager, next, jumper"
          ></el-pagination>
        </div>
        <!-- end -->
         <!-- 新增标签弹层 -->
        <Dialog ref="editUser" v-model="formData.subjectID"
        :titleInfo="titleInfo" :formBase="formData" @newDataes="loadDire"></Dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
// 引入状态显示
import Breadcrumb from '@/components/Breadcrumb/newBreadcrumb'
import { status } from '@/api/hmmm/constants'
import { list, remove, detail, changeState } from '@/api/hmmm/directorys.js'
// 弹出层
import Dialog from './../components/directorys-add'
export default {
  name: 'directorys',
  components: {
    Dialog,
    Breadcrumb
  },
  data () {
    return {
      alertText: '',
      total: '',
      //  loadList: false,
      listLoading: false,
      requestParameters: {
        page: 1,
        pagesize: 10
      },
      dataList: [],
      titleInfo: {
        pageTitle: '目录', // 页面标题
        text: '' // 新增、编辑文本
      },
      formData: {
        subjectName: '', // 所属学科
        directoryName: ''// 目录名称
      }
    }
  },
  created () {
    this.loadDire()
  },
  computed: {
    status () {
      return status
    }
  },
  methods: {
    async loadDire () {
      this.listLoading = true
      // 获取路由穿过来的参数
      //  console.log(this.$route.query)
      if (this.$route.query.id) {
        this.requestParameters.subjectID = this.$route.query.id
      } else {
      }
      const { data: res } = await list(this.requestParameters)
      this.listLoading = false
      this.total = res.counts
      this.alertText = `共 ${this.total} 条记录`
      this.dataList = res.items
      this.total = res.counts
      // console.log(11, this.dataList)
    },
    // 每页显示信息条数
    handleSizeChange (val) {
      this.requestParameters.pagesize = val
      if (this.requestParameters.page === 1) {
        this.loadDire(this.requestParameters)
      }
    },
    // 进入某一页
    handleCurrentChange (val) {
      this.requestParameters.page = val
      this.loadDire()
    },
    // 禁用 开启
    async statusChange (row) {
      // console.log(row)
      let status = ''
      if (row.state === 1) {
        status = '禁用'
        row.state = 0
      } else {
        status = '开启'
        row.state = 1
      }
      this.$confirm('已成功' + status + ', 是否继续?', '提示',
        {
          type: 'warning'
        }).then(async () => {
        await changeState({ id: row.id, state: row.state })
          .then(response => {
            this.$message.success('已成功' + status + '!')
            this.loadDire(this.requestParameters)
          })
          .catch(response => {
            this.$message.error(status + '失败!')
          })
      }).catch(() => {
        this.$message.info('已取消操作!')
      })
    },
    // 删除
    removeSub (id) {
      this.$confirm('此操作将永久删除 ' + ', 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        try {
          const res = await remove({ id })
          console.log(res)
          this.$message.success('删除成功!')
          this.loadDire()
        } catch (error) {
          console.log(error)
          this.$message.error('删除失败!')
        }
      }).catch(() => {
      })
    },
    // 点击弹出层
    // **********************************
    // 搜索的项目
    // 添加、编辑之后表单清空
    query () {
      this.formData = {}
    },
    // 新增用户、编辑用户
    handleChange (id) {
      this.query()
      this.$refs.editUser.dialogFormV()
      if (id === 'add') {
        this.titleInfo.text = '创建' // 给弹层传参
      } else {
        this.titleInfo.text = '编辑' // 给弹层传参
        this.hanldeEditForm(id)
      }
    },
    // 窗口操作**********************************
    // 编辑
    // 表单详情数据加载
    async hanldeEditForm (objeditId) {
      this.formData.id = objeditId
      const { data: res } = await detail({ id: objeditId })
      // 获取详情 给formData
      if (res.isFrontDisplay === 1) {
        res.isFrontDisplay = true
      } else {
        res.isFrontDisplay = false
      }
      this.formData = res
    },
    // 搜索
    serschSub () {
      this.requestParameters.page = 1
      this.loadDire(this.requestParameters)
    },
    // 重置
    resetForm () {
      this.requestParameters = {
        page: 1
      }
      this.loadDire(this.requestParameters)
    }
  }

}
</script>

<style>
.butAdd {
  float: right;
}
.el-table .el-link {
 font-size: 12px;
 }
</style>
